package com.hundun.hddeal.service;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.hundun.hddeal.entity.AdUser;
import com.hundun.hddeal.entity.MtUser;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service("TokenService")
public class TokenService {

    /**
     * 公共密钥
     */
     public static String SECRET="FreeMaNong";



    // 过期时间，单位毫秒
    //private static final long EXPIRE_TIME = 20;
    // 生成过期时间
    //Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);

    public String getToken(String  userId) {
       /* String token = JWT.create().withExpiresAt(date).withAudience(mtUser.getMtId())
                .sign(Algorithm.HMAC256(mtUser.getMtPwd()));
        return token;*/
        //签发时间
        Date iatDate =new Date();

        //过期时间- 1分钟过期
        Calendar nowTime =Calendar.getInstance();
        nowTime.add(Calendar.HOUR_OF_DAY,5);//HOUR_OF_DAY
        Date expiresDate=nowTime.getTime();

        Map<String,Object> map=new HashMap<>();
        map.put("alg","HS256");
        map.put("type","JWT");
        String token=JWT.create().withExpiresAt(expiresDate).withClaim("name",userId).withIssuedAt(iatDate)
                .sign(Algorithm.HMAC256(SECRET));
        return token;

    }


    public  String createToken(AdUser adUser){

       return  null;
    }



    //解密
   /* public  Map<String, Claim> verifyToken(String token){
        JWTVerifier verifier=JWT.require(Algorithm.HMAC256(SECRET)).build();
        DecodedJWT jwt;
        jwt=verifier.verify(token);
        return jwt.getClaims();
    }*/






}
